package cn.yunyichina.provider.order.service.impl;

import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.base.OrgApp;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.order.entity.AsmQueryOrder;
import cn.yunyichina.provider.order.entity.vo.AsmQueryOrderRequest;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JoeZ on 2017/3/2.
 */
@Service
public class AsmOrderServiceImpl {


    private Logger logger = LoggerFactory.getLogger(AsmOrderServiceImpl.class);

    /**
     * 支付记录查询
     * @param data
     * @param sessionId
     * @interfaceName: yyt.order.asm.order.query
     * @return
     */
    public Response queryAsmOrder(String data, String sessionId){
        Response response = new Response();
        logger.info("queryAsmOrder, request data=" + data + ", sessionId=" + sessionId);
        AsmQueryOrderRequest request = JsonUtils.parseObject(data, AsmQueryOrderRequest.class);
        Map<String, String> params = new HashMap<>();

        try {
            Validator.getInstance().validate(request);
        } catch (ValidateException ex) {
            logger.info("queryAsmOrder 数据校验失败:" + ex.getMessage() + "|入参:" + data + "！");
            response.setResultCodeAndMessage("13", "数据校验失败:" + ex.getMessage() + "！");
            return response;
        }

        //获得platform
        OrgApp orgApp = null;
        Map<String, String> map = new HashMap<>();
        map.put("org_app_code",request.getHospitalAppCode());
        response = BaseService.callHumpFromInternal("yyt.base.orgapp.get" , JsonUtils.toJsonString(map));
        if("0".equals(response.getResultCode())){
            orgApp = JsonUtils.parseObject(response.getResult(), OrgApp.class);
            params.put("platform_org_app_code", StringUtils.isBlank(orgApp.getOrgAppCode()) ? null : orgApp.getOrgAppCode());
        }else{
            response.setResultCodeAndMessage("1", "hospitalAppCode error!");
            return response;
        }

        params.put("org_code", StringUtils.isBlank(request.getHospitalCode()) ? null : request.getHospitalCode());
        params.put("branch_code", StringUtils.isBlank(request.getBranchCode()) ? null : request.getBranchCode());
        params.put("order_no",StringUtils.isBlank(request.getYytOrderNo()) ? null : request.getYytOrderNo());
        params.put("out_order_no",StringUtils.isBlank(request.getOutOrderNo()) ? null : request.getOutOrderNo());
        params.put("payment_deal_id",StringUtils.isBlank(request.getPsOrdnum()) ? null : request.getPsOrdnum());

        if(StringUtils.isNotBlank(request.getBeginDate())){
            params.put("begin_created_time",request.getBeginDate());
        }
        if(StringUtils.isNotBlank(request.getEndDate())){
            params.put("end_created_time",request.getEndDate());
        }

        response = BaseService.callHumpFromInternal("yyt.base.order.list.get" , JsonUtils.toJsonString(params));
        if("0".equals(response.getResultCode())){
            List<OrderExtVo> orders = JsonUtils.parseArray(response.getResult(), OrderExtVo.class);
            List<AsmQueryOrder> amsOrders = new ArrayList<AsmQueryOrder>();
            for (OrderExtVo order : orders) {
                AsmQueryOrder asmQueryOrder = new AsmQueryOrder();
                asmQueryOrder.setYytOrderNo(order.getOrderNo());
                asmQueryOrder.setPsOrdnum(order.getPaymentDealId());
                asmQueryOrder.setOutOrderNo(order.getOutOrderNo());
                asmQueryOrder.setPayAmount(order.getPayAmount());
                asmQueryOrder.setPayTime(order.getPayTime());

                int payStatus = 0;
                if(order.getPayStatus() == 3){
                    payStatus = 1;
                }else if(order.getPayStatus() == 7){
                    payStatus = 2;
                }

                asmQueryOrder.setPayStatus(payStatus);
                amsOrders.add(asmQueryOrder);
            }
            response.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE , Response.SUCCESS_MESSAGE , JsonUtils.toJsonString(amsOrders));
        }else{
            response.setResultCodeAndMessage("1", "get order list error!");
            return response;
        }

        return response;
    }
}
